package com.dq.controller.api.merchant.orders;

import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.orders.UserOrderReturnQuery;
import com.dq.controller.in.orders.UserOrderReturnSave;
import com.dq.vo.Vo;
import com.dq.domain.orders.UserOrderReturn;
import com.dq.domain.sys.user.user.OrgUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author cf
 */
@Api(description = "门店 退货单管理")
@RestController
@RequestMapping("app/orgApi/userOrderReturn/")
public class ApiOrgUserOrderReturnController extends AllService {

    @ApiOperation("退货单列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:userOrderReturn:list")
    public Vo list(@ApiParam("") @RequestBody UserOrderReturnQuery userOrderReturnQuery) throws AdminException {

        if(userOrderReturnQuery.getOrgId() == null) {
            SysUser user = sysUserService.getUser();
            if(user.getSection() == null || user.getSection().getId() == null) {
                throw new AdminException("数据异常， 登录用户所属门店为空");
            }
            userOrderReturnQuery.setOrgId(user.getSection().getId());
        }

        return userOrderReturnService.list(userOrderReturnQuery);
    }

    @ApiOperation("获取退货单详情")
    @GetMapping("/getInfo/{userOrderId}")
    @RequiresPermissions("sys:userOrderReturn:list")
    public Vo getInfo(@PathVariable @ApiParam(value = "退货单id") Long userOrderId) throws AdminException {

        return new Vo(userOrderReturnService.getUserOrderInfo(userOrderId));
    }

    @ApiOperation("门店手动录入退货单")
    @PostMapping("/save")
    @RequiresPermissions("sys:userOrderReturn:save")
    public Vo save(@ApiParam("") @RequestBody UserOrderReturnSave userOrderReturnSave) throws AdminException {
        SysUser user = sysUserService.getUser();
        UserOrderReturn.OrderReStatus orderReStatus = null;
        String msg = "";
        if(user instanceof OrgUser) {
            orderReStatus = UserOrderReturn.OrderReStatus.end;
            msg = "角色为管理者，退货完成";
        }else {
            orderReStatus = UserOrderReturn.OrderReStatus.waitPass;
            msg = "角色非管理者， 退货等待审核";
        }
        // 手动录入退货单， 状态 根据登录用户是否为门店
        userOrderReturnService.manualSave(userOrderReturnSave, orderReStatus);

        return new Vo(HttpStatus.OK, msg);
    }

    @ApiOperation("退单审批/退单完成")
    @PostMapping("/examine/{id}/{status}")
    @RequiresPermissions("sys:userOrderReturn:update")
    public Vo examine(@PathVariable @ApiParam(value = "退货单id") Long id,
                      @PathVariable @ApiParam(value = "审批状态(pass: 通过,noPass: 不通过,end: 订单完成)") UserOrderReturn.OrderReStatus status) throws AdminException {

        userOrderReturnService.examine(id, status, "");
        return new Vo(HttpStatus.OK);
    }
    @ApiOperation("修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:userOrderReturn:update")
    public Vo update(@RequestBody @ApiParam(value = "") UserOrderReturn userOrderReturn) throws AdminException {
        if(userOrderReturn.getId() == null) {
            throw new AdminException("缺参id");
        }

        UserOrderReturn orgPositionQuery = dao.get(UserOrderReturn.class, userOrderReturn.getId());
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }
        BeanUtils.copyProperties(userOrderReturn, orgPositionQuery, BeanNullUtil.getNullPropertyNames(userOrderReturn));
        userOrderReturnService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @SysLog("删除")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("sys:userOrderReturn:delete")
    public Vo delete(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        UserOrderReturn orgPositionQuery = dao.get(UserOrderReturn.class, id);
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }

        orgPositionQuery.setDeleted(true);
        userOrderReturnService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }

}
